<template>
	<view class="user" :class="[isDark?'dark':'', $store.state.AppStyle]" :style="{'background-color':isDark?'#1c1c1c':'#f6f6f6','min-height':isDark?'100vh':'auto'}">
		<view class="header" :style="[{height:CustomBar + 'px'}]">
			<view class="cu-bar" :class="isDark?'dark':'bg-white'" :style="{'height': CustomBar + 'px','padding-top':StatusBar + 'px','background-color':isDark?'#1c1c1c':'#ffffff'}">
				<view class="action" @tap="back">
					<text class="cuIcon-back"></text>
				</view>
				<view class="content text-bold" :style="[{top:StatusBar + 'px'}]">
					<block v-if="type=='fk'">
					{{$t('user.help.toadminfeedback')}}
					</block>
					<block v-else>
					{{$t('user.help.reporttomgr')}}
					</block>
				</view>
				<view class="action">
				</view>
			</view>
		</view>
		<view :style="[{padding:NavBar + 'px 10px 0px 10px'}]"></view>
		<block v-if="type=='fk'">
		<view class="about-tips">
			{{$t('user.help.tomyfeedback')}}<br /><br />
			<text style="color: orangered;">{{$t('user.help.illegalcontent')}}</text>
		</view>
		</block>
		<view class="cu-list menu-avatar userList" style="margin-top: 20upx;">
			<view class="no-data" v-if="userList.length==0">
				<text class="cuIcon-text"></text>{{$t('common.nodata')}}
			</view>
			<view class="cu-item" v-for="(item,index) in userList" :key="index">
				<view class="cu-avatar round lg" :style="item.style"></view>
				<view class="content">
					<view class="text-grey">
						<block  v-if="item.screenName">{{item.screenName}}</block>
						<block  v-else>{{item.name}}</block>
					</view>
					<view class="text-gray text-sm flex">
						<view class="text-cut">
							{{subText(item.introduce,100)}}
						</view>
					</view>
				</view>
				<view class="action goUserIndex" @tap="getPrivateChat(item)">
				<block v-if="type=='fk'">
					<view class="cu-btn bg-gradual-green" style="font-size: 26upx;height: 55upx;border-radius: 100upx;">{{$t('user.help.feedback')}}</view>
				</block>
				<block v-else>
					<view class="cu-btn bg-gradual-green" style="font-size: 26upx;height: 55upx;border-radius: 100upx;">{{$t('common.select')}}</view>
				</block>
				</view>
			</view>
		</view>
		<tn-modal v-model="textShow" showCloseBtn :custom="true">
			<view class="custom-modal-content">
				<form>
					<view class="margin-top">
						<view class="title margin-bottom" style="font-size: 30upx;">{{$t('user.help.reasonreport')}}</view>
						<textarea maxlength="1000" style="font-size: 30upx;" v-model="text"
							placeholder="$t('user.help.plsreasonreport')" name="text"></textarea>
					</view>
				</form>
				<tn-button padding="40rpx 30rpx" width="100%" backgroundColor="tn-main-gradient-blue--light"
					style="margin:10upx 0 30upx 0" @click="setTextShow()">{{$t('common.sure')}}</tn-button>
			</view>
		</tn-modal>
		<!--加载遮罩-->
		<view class="loading" v-if="isLoading==0">
			<view class="loading-main">
				<image src="../../static/loading.gif"></image>
			</view>
		</view>
		<!--加载遮罩结束-->
	</view>
</template>

<script>
	import { localStorage } from '../../js_sdk/mp-storage/mp-storage/index.js'
	import darkModeMixin from '@/utils/darkModeMixin.js'
	export default {
		mixins: [darkModeMixin],
		data() {
			return {
				StatusBar: this.StatusBar,
				CustomBar: this.CustomBar,
				NavBar:this.StatusBar +  this.CustomBar,
				AppStyle:this.$store.state.AppStyle,
				type:"fk",
				title:"",
				shop:"",
				user:"",
				userList:[],
				text:"",
				textShow:false,
				page:1,
				moreText:$t('common.loadmoretxt'),
				isLoad:0,
				isLoading:0,
				
			}
		},
		onPullDownRefresh(){
			var that = this;
			that.page=1;
			that.getUserList(false);
			setTimeout(function () {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onShow(){
			var that = this;
			that.page=1;
			// #ifdef APP-PLUS
			
			//plus.navigator.setStatusBarStyle("dark")
			// #endif
			
		},
		onLoad(res) {
			var that = this;
			// #ifdef APP-PLUS || MP
			that.NavBar = that.CustomBar;
			// #endif
			if(res.type){
				that.type = res.type
				if(that.type!='fk'){
					that.textShow = true
				}
			}
			if(res.title){
				that.title = res.title
			}
			that.getUserList(false);
		},
		methods:{
			allCache(){
				var that = this;
				if(localStorage.getItem('userList')){
					that.userList = JSON.parse(localStorage.getItem('userList'));
				}
			},
			back(){
				uni.navigateBack({
					delta: 1
				});
			},
			formatDate(datetime) {
				var datetime = new Date(parseInt(datetime * 1000));
				// 获取年月日时分秒值  slice(-2)过滤掉大于10日期前面的0
				var year = datetime.getFullYear(),
					month = ("0" + (datetime.getMonth() + 1)).slice(-2),
					date = ("0" + datetime.getDate()).slice(-2),
					hour = ("0" + datetime.getHours()).slice(-2),
					minute = ("0" + datetime.getMinutes()).slice(-2);
				//second = ("0" + date.getSeconds()).slice(-2);
				// 拼接
				var result = year + "-" + month + "-" + date + " " + hour + ":" + minute;
				// 返回
				return result;
			},
			loadMore(){
				var that = this;
				that.moreText = that.$t('global.loading');
				that.isLoad=1;
				that.getUserList(true);
				
			},
			getUserList(isPage){
				var that = this;
				var page = that.page;
				var data = {
					"groupKey":"administrator"
				}
				var token = ""
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token=userInfo.token;
				
				}
				that.$Net.request({
					url: that.$API.getUserList(),
					data:{
						"searchParams":data,
						"limit":10,
						"page":1,
						"order":"created",
						"token":token
					},
					header:{
						'Content-Type':'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						that.isLoad=0;
						if(res.data.code==1){
							var list = res.data.data;
							if(list.length>0){
								
								var userList = [];
								for(var i in list){
									var arr = list[i];
									arr.style = "background-image:url("+list[i].avatar+");"
									userList.push(arr);
								}
								that.userList = userList;
							}
						}
						var timer = setTimeout(function() {
							that.isLoading=1;
							clearTimeout('timer')
						}, 300)
					},
					fail: function(res) {
						that.isLoad=0;
						var timer = setTimeout(function() {
							that.isLoading=1;
							clearTimeout('timer')
						}, 300)
					}
				})
			},
			toUserContents(data){
				var that = this;
				var name = data.name;
				var title = data.name + that.$t('common.nameinfo');
				if(data.screenName){
					title = data.screenName + that.$t('common.nameinfo');
					name = data.screenName
				}
				var id= data.uid;
				var type="user";
				uni.navigateTo({
				    url: '/pages/contents/userinfo?title='+title+"&name="+name+"&uid="+id+"&avatar="+encodeURIComponent(data.avatar)
				});
			},
			subText(text,num){
				if(text){
					if(text.length>num){
						text = text.substring(0,num);
						return text+"……";
					}else{
						return text;
					}
				}else{
					return this.$t('global.nopersoninfo')
				}
			},
			setTextShow(){
				var that = this;
				that.textShow = false
				uni.showToast({
					title: this.$t('user.help.selsentbyadmin'),
					icon: 'none'
				})
			},
			getPrivateChat(item){
				var that = this;
				var token = "";
				var myUid = 0;
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token=userInfo.token;
					myUid=userInfo.uid;
				}else{
					uni.showToast({
						title: that.$t('common.plslogin'),
						icon: 'none'
					})
					uni.navigateTo({
						url: '/pages/user/login'
					});
					return false;
				}
				var touid = item.uid;
				if(myUid==touid){
					uni.showToast({
						title: that.$t('user.help.nooneselfchat'),
						icon: 'none'
					})
					return false;
				}
				var data={
					"touid":touid,
					"token":token
				}
				uni.showLoading({
					title: that.$t('common.loadmore')
				});
				that.$Net.request({
					url: that.$API.getPrivateChat(),
					data:data,
					header:{
						'Content-Type':'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						setTimeout(function () {
							uni.hideLoading();
						}, 1000);
						
						if(res.data.code==1){
							var name = item.name;
							if(item.screenName!=""&item.screenName!=null){
								name = item.screenName;
							}
							var uid = item.uid;
							var chatid = res.data.data
							if(that.type=='shop'){
								uni.redirectTo({
								    url: '/pages/chat/chat?uid='+uid+"&name="+name+"&chatid="+chatid+"&jb=shop&title="+that.title+"&text="+that.text
								});
							}else if(that.type=='user'){
								uni.redirectTo({
								    url: '/pages/chat/chat?uid='+uid+"&name="+name+"&chatid="+chatid+"&jb=user&title="+that.title+"&text="+that.text
								});
							}else if(that.type=='group'){
								uni.redirectTo({
								    url: '/pages/chat/chat?uid='+uid+"&name="+name+"&chatid="+chatid+"&jb=group&title="+that.title+"&text="+that.text
								});
							}else if(that.type=='app'){
								uni.redirectTo({
								    url: '/pages/chat/chat?uid='+uid+"&name="+name+"&chatid="+chatid+"&jb=app&title="+that.title+"&text="+that.text
								});
							}else if(that.type=='fk'){
								uni.redirectTo({
								    url: '/pages/chat/chat?uid='+uid+"&name="+name+"&chatid="+chatid
								});
							}else{
								uni.redirectTo({
								    url: '/pages/chat/chat?uid='+uid+"&name="+name+"&chatid="+chatid+"&jb=text&title="+that.title+"&text="+that.text
								});
							}
							
						}else{
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						}
					},
					fail: function(res) {
						setTimeout(function () {
							uni.hideLoading();
						}, 1000);
						uni.showToast({
							title: that.$t('common.networkfail'),
							icon: 'none'
						})
					}
				})
			},
		}
	}
</script>
<style>
</style>